﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CaseManagerLibrary.WinForm;
using PSDLib.Models;
using CaseManagerLibrary.Models;
using PSDLib;

namespace PSDManager
{
    public interface IDataPanel
    {
        void Reset(bool immediate);
        void RefreshData(params object[] parameter);
    }
    public partial class DownloadInfoPanel : UserControl, IDataPanel
    {
        public delegate void UpdateSummaryEventHandler(dl_task task, dl_task_channel channel, int fileCount);
        public event UpdateSummaryEventHandler OnUpdateSummary;

        public dl_task Task { get; set; }
        public dl_task_channel Channel { get; set; }

        public ManagerHelper ManagerHelper { get; set; }

        public bool ShowListCheckBox
        {
            get { return lvwDownload.CheckBoxes; }
            set { lvwDownload.CheckBoxes = value; }
        }

        public int[] FileState { get; set; }
        public string Summary
        {
            get { return this.lblSummary.Text; }
            set { this.lblSummary.Text = value; }
        }

        public DownloadInfoPanel()
        {
            InitializeComponent();

            this.toolStrip1.Visible = false;
            this.ShowListCheckBox = false;
            this.paging.PageSize = 50;
            this.paging.OnPageChanged += Paging_OnPageChanged;

           
        }

        public void Reset(bool immediate = true)
        {
            Summary = "";
            lvwDownload.Items.Clear();
            paging.Reset();
        }

        public void AddCustomAction(string name, Image image, Action<ToolStripButton, ListView> action)
        {
            var miAction = new ToolStripButton();
            miAction.Text = name;
            miAction.Image = image;
            miAction.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
            miAction.TextImageRelation = TextImageRelation.ImageAboveText;
            miAction.Click += new EventHandler(delegate (object sender, EventArgs e)
            {
                action?.Invoke(miAction, lvwDownload);
            });
            this.toolStrip1.Items.Add(miAction);
            this.toolStrip1.Visible = true;
        }

        public void RefreshData(params object[] parameter)
        {
            if (parameter.Length > 0)
            {
                Task = parameter[0] as dl_task;
            }
            else
            {
                Task = null;
            }

            if (parameter.Length > 1)
            {
                Channel = parameter[1] as dl_task_channel;
            }
            else
            {
                Channel = null;
            }

            paging.PageNo = 1;
        }

        private void Paging_OnPageChanged(int pageNo, int pageSize)
        {
            PageResult<dl_files> files = null;
            this.lvwDownload.SuspendLayout();
            this.lvwDownload.Items.Clear();

            if (Task != null)
            {
                files = ManagerHelper.GetPSDDownloadDetail(
                    Task.download_device_id,
                    Task.id,
                    (Channel == null ? "" : Channel.id),
                    FileState,
                    pageNo,
                    pageSize);

                int total = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(files.Count) / pageSize));
                paging.Total = total > 0 ? total : 1;

                OnUpdateSummary?.Invoke(Task, Channel, files.Count);

                if (files.Entities != null)
                {
                    int index = 1;
                    foreach (dl_files f in files.Entities)
                    {
                        string status = "";
                        if (f.state == -1)
                        {
                            status += "等待下载";
                        }
                        else if (f.state == 0)
                        {
                            status += "正在下载";
                        }
                        else if (f.state == 1)
                        {
                            status += "下载速度：" + PSDFunction.FormatByteSize(f.dl_speed) + "/s";
                            status += "  文件大小：" + PSDFunction.FormatByteSize(f.file_size);
                        }
                        else if (f.state == 2)
                        {
                            status += f.error_log;
                        }

                        lvwDownload.Items.Add(new ListViewItem(new string[] {
                            (index + (pageNo-1)*pageSize).ToString(),
                            f.channel_name,
                            f.video_start_time.ToString("yyyy-MM-dd HH:mm:ss") + " 至 " + f.video_end_time.ToString("yyyy-MM-dd HH:mm:ss"),
                            f.dl_start_time.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                            status,
                            f.file_path
                        })).Tag = f;

                        index += 1;
                    }
                }
            }

            this.lvwDownload.ResumeLayout();
        }
    }
}
